package org.xxpay.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.ZolozAuthenticationCustomerSmilepayInitializeRequest;
import com.alipay.api.response.ZolozAuthenticationCustomerSmilepayInitializeResponse;
import org.apache.dubbo.config.annotation.Service;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.service.IXxPayAlipayApiService;
import org.xxpay.pay.channel.alipay.AlipayConfig;

/**
 * @author: dingzhiwei
 * @date: 2019-07-06
 * @description:
 */
@Service(interfaceName = "org.xxpay.core.service.IXxPayAlipayApiService", version = Constant.XXPAY_SERVICE_VERSION, retries = Constant.DUBBO_RETRIES)
public class XxPayAlipayApiServiceImpl implements IXxPayAlipayApiService {

    private static final MyLog _log = MyLog.getLog(XxPayAlipayApiServiceImpl.class);

    @Override
    public JSONObject authenticationSmilepayInitialize(String alipayPayParams, String metaInfo) {

        AlipayConfig alipayConfig = new AlipayConfig(alipayPayParams);

        ZolozAuthenticationCustomerSmilepayInitializeRequest req
                = new ZolozAuthenticationCustomerSmilepayInitializeRequest();

        req.setBizContent(metaInfo);
        try {
            ZolozAuthenticationCustomerSmilepayInitializeResponse resp = AlipayConfig.alipayExecute(alipayConfig, req);
            if(resp.isSuccess()) {
                String result = resp.getResult();
                JSONObject resultJson = JSON.parseObject(result);
                return resultJson;
            }
        } catch (AlipayApiException e) {
            _log.error(e, "");
        } catch (Exception e) {
            _log.error(e, "");
        }

        return null;
    }

}
